package com.gui.listeners;

import com.util.ResourceLoader;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

/**
 * Listener class for the CustomTableModel instance bound to routesTable JTable
 * component.
 * @author sithum
 */
public class RoutesTableModelListener implements TableModelListener {

    JTextField routeName;
    JTable table;
    int reflectColumn;

    public RoutesTableModelListener(JTextField routeName, JTable table) {
        this.routeName = routeName;
        this.table = table;
        reflectColumn = Integer.parseInt(ResourceLoader.getInstance().getProperty("category.edit.reflect"));
        table.getModel().addTableModelListener(this);
    }

    public void tableChanged(TableModelEvent e) {
        int selectedRow = e.getFirstRow();
        int selectedColumn = e.getColumn();
        Object changedValue = table.getValueAt(selectedRow, selectedColumn);
        if (changedValue.toString().equalsIgnoreCase("true")) {
            routeName.setText(table.getValueAt(selectedRow, reflectColumn).toString());
        } else if (changedValue.toString().equalsIgnoreCase("false")) {
            routeName.setText("");
        }
    }
}
